﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Common;
using Common.Admin;
using Common.Controls;
using System.Windows.Markup;

namespace SLAdmin.UserControls
{
    public partial class EditSurveyPage : UserControl
    {
        DialogHelper _dialogHelper = new DialogHelper();
        StudyManager _studyManager = StudyManager.GetInstance();
        StudyDataClient _studyDataClient = new StudyDataClient();

        SurveyPage _surveyPage;
        int _surveyPageId;
        public delegate void SurveyPageUpdatedDelegate();
        public event SurveyPageUpdatedDelegate SurveyPageUpdated;
        public EditSurveyPage()
        {
            InitializeComponent();
        }
        public EditSurveyPage(SurveyPage SurveyPage)
        {
            InitializeComponent();
            _surveyPage = SurveyPage;
            _surveyPageId = SurveyPage.SurveyPageId;
            this.DataContext = _surveyPage;            
            BindPreview();
        }
        private void BindPreview()
        {
            try
            {
                string Content = StudyManager.XamlInitializationString.Replace("{0}", (this.DataContext as SurveyPage).PageContent); 
                Xaml.Content = XamlReader.Load(Content);
                Xaml2.Content = XamlReader.Load(Content);                
            }
            catch { }
            finally
            {
                
            }
        }
        private void BindPreview2()
        {
            try
            {
                string Content = StudyManager.XamlInitializationString.Replace("{0}", rtCode.Text);
                Xaml.Content = XamlReader.Load(Content);
                Xaml2.Content = XamlReader.Load(Content);                
            }
            catch { }
            finally
            {

            }
        }

        private void rtCode_LostFocus(object sender, RoutedEventArgs e)
        {
            BindPreview2();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            _studyDataClient.UpdateSurveyPage(_surveyPage);
            SurveyPageUpdated();
        }

        private void rtCode_TextChanged(object sender, TextChangedEventArgs e)
        {
            BindPreview2();
        }

        private void UserControl_LayoutUpdated(object sender, EventArgs e)
        {
            CodeTabs.Width = this.ActualWidth;
        }

    }
}
